<template>
  <view class="myLogin">
      <uni-icons type="contact-filled" size="100" color="#afafaf"></uni-icons>
      <button open-type="getUserInfo" @getuserinfo="getUserInfo" class="myLogin-btn">一键登录</button>
      <text class="myLogin-txt">🛒登录后尽享更多权益🛒</text>
  </view>
</template>

<script>
  import {mapMutations,mapState} from 'vuex'
  export default {
    name:"my-login",
    data() {
      return {
        
      };
    },
    computed:{
      ...mapState('user',['redirectInfo'])
    },
    methods:{
      ...mapMutations('user',['setUserInfo','changeToken']),
      getUserInfo(e){
        console.log(e);
        if(!e.detail.errMsg === "getUserInfo:ok") return uni.$showMsg('获取用户信息失败')
        this.setUserInfo(e.detail.userInfo)
        this.getData(e.detail)
      },
     async getData(info){
        const [err,res] = await uni.login()
        // console.log(res);
        if(res.errMsg !== "login:ok") return uni.$showMsg('登陆失败')
       const res1 = await uni.$http.post('/api/public/v1/users/wxlogin',{...info,code:res.code}).catch(err=>err)
       // console.log('res1',res1);
       this.changeToken(res1.message?res1.message:'11111')
       uni.showToast({
         title:'登录成功',
         icon:'success'
       })
       this.toPath()
      },
      toPath(){
        if(this.redirectInfo && this.redirectInfo.opentype === 'switchTab'){
          uni.switchTab({
            url:this.redirectInfo.from
          })
        }
      }
    }
  }
</script>

<style lang="scss">
.myLogin{
  text-align: center;
  padding: 200rpx 40rpx 0;
  &-btn{
    background-color: #c00000;
    border-radius: 80rpx;
    margin: 40rpx 0 20rpx;
    color: #fff;
  }
  &-txt{
    color: #999;
    font-size: 12px;
  }
}
</style>